<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/11/26
 * Time: 22:52
 */

namespace app\storage\model;


use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;

class BomDetailModel extends Model
{

    protected $table = 'iwh_bom_detail';
    protected $pk = 'uuid';


    public function Materiel()
    {
        return $this->belongsTo('MaterielModel', 'sub_materiel_id', 'sub_materiel_id')->bind('sub_materiel_name,format,basic_unit');
    }


    /**
     * 根据bomUuid查询数据
     *
     * @param $condition
     * @return false|\PDOStatement|string|\think\Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws \think\Exception
     */
    public function getBomDetail($condition)
    {
        $outflowModel = new OutflowModel();
        $where = [
            'order_id' => $condition['order_id'],
            'fentryid' => $condition['fentryid'],
        ];
        $bomModel = new BomModel();
        $bomInfo = $bomModel->where($where)->find()->toArray();
        $supplierUuid = $bomInfo['supplier_uuid'];
        $storageUuid = $bomInfo['storage_uuid'];
        if (!empty($storageUuid)) {
            $map['storage_uuid'] = $storageUuid;
        } else {
            $map['storage_uuid'] = empty($condition['storage_uuid']) ? [] : $condition['storage_uuid'];
        }
        if (!empty($supplierUuid)) {
            $supplierModel = new SupplierModel();
            $supplier = $supplierModel->alias('s')->field("sub_materiel_id,sum(count) as s_count")
                ->join(['iwh_supplier_materiel' => 'sm'], 'sm.supplier_id = s.supplier_id', 'left')
                ->group('sub_materiel_id')
                ->where(['s.uuid' => $supplierUuid])
                ->select(false);
        } else {
            $supplierStorageModel = new SupplierStorageModel();
            $supplier = $supplierStorageModel->alias('ss')->field("sub_materiel_id,sum(count) as s_count")
                ->join(['iwh_supplier' => 'p'], 'ss.supplier_uuid = p.uuid', 'left')
                ->join(['iwh_supplier_materiel' => 'sm'], 'sm.supplier_id = p.supplier_id', 'left')
                ->group('sub_materiel_id')
                ->where($map)
                ->select(false);
        }
        $reelsModel = new ReelsModel();
        $reels = $reelsModel->field("sub_materiel_id,sum(count) as r_count")
            ->join(['iwh_supplier_materiel'])
            ->group('sub_materiel_id')
//            ->where($map)
            ->select(false);

        $outflow = $outflowModel->field("sub_materiel_id,sum(count) as send")
            ->group('sub_materiel_id')
            ->where($where)
            ->select(false);

        $result = $this->alias('a')
            ->field("a.*,m.sub_materiel_name,m.basic_unit,m.format,ifnull(b.send,0) as send,a.count-ifnull(b.send,0) as diff,ifnull(r_count,0) as r_count,ifnull(s_count,0) as s_count")
            ->join(['iwh_sub_materiel' => 'm'], 'a.sub_materiel_id = m.sub_materiel_id', 'left')
            ->join(["($outflow)" => 'b'], 'a.sub_materiel_id = b.sub_materiel_id', 'left')
            ->join(["($reels)" => 'c'], 'c.sub_materiel_id = a.sub_materiel_id', 'left')
            ->join(["($supplier)" => 'd'], 'd.sub_materiel_id = a.sub_materiel_id', 'left')
            ->order('diff desc,a.sub_materiel_id asc')
            ->where('bom_uuid', $condition['bom_uuid'])
            ->select();


        if (count($result)) {
            $result = $result->toArray();
            foreach ($result as $key => $item) {
                $count_1 = $item['s_count'] - $item['diff'];
                $count_2 = $item['r_count'] - $item['diff'];
                if ($item['diff'] < 0 || $count_1 > 0 || $count_2 > 0) {
                    $result[$key]['kitting'] = 1;
                } else {
                    $result[$key]['kitting'] = 0;
                }
            }
            $kitting = array_column($result, 'kitting');
            array_multisort($kitting, SORT_ASC, $result);
        }

        return $result;
    }

}